package com.qf.controller;

import com.qf.dao.MyESRep;
import com.qf.pojo.SmsSendLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/es")
public class EsController {

    private MyESRep myESRep;

    @Autowired
    public void setMyESRep(MyESRep myESRep) {
        this.myESRep = myESRep;
    }

    @GetMapping("/log/{id}")
    public SmsSendLog  getLogById(@PathVariable Long id){
        System.out.println("000000000");
        Optional<SmsSendLog> byId = myESRep.findById(id);
        if(byId!=null){
            SmsSendLog smsSendLog = byId.get();
            return smsSendLog;
        }
        return null;
    }


    @GetMapping("/all")
    public List<SmsSendLog> findAll(){
        System.out.println("all 进来了");
        Iterable<SmsSendLog> all = myESRep.findAll();
        List<SmsSendLog> smsSendLogs = new ArrayList<>();

        all.forEach(smsSendLog->{
            smsSendLogs.add(smsSendLog);
        });
        return smsSendLogs;
    }

    @GetMapping("/name")
    public List<SmsSendLog> getLogByName(String name){

        List<SmsSendLog> byCorpName = myESRep.findByCorpName(name);
        return byCorpName;

    }




}
